/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.alibaba.excel.EasyExcel;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.CourseClassStudent;
import me.zhengjie.modules.system.domain.StudentBatchAddRequest;
import me.zhengjie.modules.system.domain.vo.CourseClassStudentVo;
import me.zhengjie.modules.system.domain.vo.ExamInfoVo;
import me.zhengjie.modules.system.listener.UploadClassStudentListener;
import me.zhengjie.modules.system.listener.UploadExamInfoListener;
import me.zhengjie.modules.system.service.CourseClassStudentService;
import me.zhengjie.modules.system.service.dto.CourseClassStudentQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author 王宇
* @date 2023-09-18
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程班级学员管理")
@RequestMapping("/api/courseClassStudent")
public class CourseClassStudentController {

    private final CourseClassStudentService courseClassStudentService;

    @Autowired
    private UploadClassStudentListener studentListener;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('courseClassStudent:list')")
    public void download(HttpServletResponse response, CourseClassStudentQueryCriteria criteria) throws IOException {
        courseClassStudentService.download(courseClassStudentService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询课程班级学员")
    @ApiOperation("查询课程班级学员")
    @PreAuthorize("@el.check('courseClassStudent:list')")
    public ResponseEntity<Object> query(CourseClassStudentQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(courseClassStudentService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增课程班级学员")
    @ApiOperation("新增课程班级学员")
    @PreAuthorize("@el.check('courseClassStudent:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CourseClassStudent resources){
        return new ResponseEntity<>(courseClassStudentService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改课程班级学员")
    @ApiOperation("修改课程班级学员")
    @PreAuthorize("@el.check('courseClassStudent:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CourseClassStudent resources){
        courseClassStudentService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除课程班级学员")
    @ApiOperation("删除课程班级学员")
    @PreAuthorize("@el.check('courseClassStudent:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        courseClassStudentService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("batchAdd")
    @Log("批量新增课程班级学员")
    @ApiOperation("批量新增课程班级学员")
    @PreAuthorize("@el.check('courseClassStudent:batchAdd')")
    public ResponseEntity<Object> batchAdd(@RequestBody StudentBatchAddRequest resources){
        boolean result = courseClassStudentService.batchAdd(resources);
        if (result){
            return new ResponseEntity<>(HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
    }

    @PostMapping("upload")
    @Log("上传课程班级学员")
    @ApiOperation("上传课程班级学员")
    @PreAuthorize("@el.check('courseClassStudent:upload')")
    public ResponseEntity<Object> upload(@RequestParam String classNo, MultipartFile file){
        try {
            UploadClassStudentListener.atmrLocal.set(classNo);
            EasyExcel.read(file.getInputStream(), CourseClassStudentVo.class, studentListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            UploadClassStudentListener.atmrLocal.remove();
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("groupNumbers")
    @Log("查询组号")
    @ApiOperation("查询组号")
    //@PreAuthorize("@el.check('courseClassStudent:list')")
    @AnonymousAccess
    public ResponseEntity<Object> groupNumbers(@RequestBody StudentBatchAddRequest resources){
        return new ResponseEntity<>(courseClassStudentService.groupNumbers(resources.getClassNo()),HttpStatus.OK);
    }
}